package com.zy.sort;

import java.util.List;

/**
 * @description: 选择排序
 * @Author zhaoyi
 * @date 2019/11/6
 */
public class ChoseSort<T extends Comparable<? super T>> {

    public void sort(List<T> list) {
        if (list == null || list.size() <= 1) {
            return;
        }

        for (int i = 0; i < list.size(); i++) {
            //选一个最小的
            T min = list.get(i);
            int index = 0;
            for (int j = (i + 1); j < list.size(); j++) {
                if (list.get(j).compareTo(min) < 0) {
                    min = list.get(j);
                    index = j;
                }
            }

            T tmp = list.get(i);
            if (min.compareTo(tmp) < 0) {
                list.set(i, min);
                list.set(index, tmp);
            }
        }
    }


}
